package com.wanmait.movie.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.wanmait.movie.entity.Cinema;
import com.wanmait.movie.entity.Combo;
import com.wanmait.movie.entity.Food;
import com.wanmait.movie.entity.FoodToCombo;
import com.wanmait.movie.entity.VO.ComboVO;
import com.wanmait.movie.mapper.ComboMapper;
import com.wanmait.movie.mapper.FoodToComboMapper;
import com.wanmait.movie.service.ComboService;
import com.wanmait.movie.service.FoodToComboService;
import org.elasticsearch.common.recycler.Recycler;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ComboServiceImpl extends ServiceImpl<ComboMapper, Combo> implements ComboService {
   @Resource
   private ComboMapper comboMapper;
   @Resource
   private FoodToComboService foodToComboService;
    @Override
    public PageInfo<ComboVO> getCombo(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        MPJLambdaWrapper<Combo> wrapper = new MPJLambdaWrapper<Combo>()
                .selectAll(Combo.class)
                .select(Cinema::getCinemaName)
                .leftJoin(Cinema.class,Cinema::getId,Combo::getCinemaId);
        return  new PageInfo<>(comboMapper.selectJoinList(ComboVO.class,wrapper));

    }

    @Override
    public boolean saveOrUpdate(ComboVO comboVO) {

        if(comboVO.getId()==null){
        List<FoodToCombo> foodToComboList = new ArrayList<>();
        comboMapper.addcombo(comboVO);
        comboVO.getFoodIdList().forEach(food->{
            FoodToCombo foodToCombo = new FoodToCombo();
            foodToCombo.setComboId(comboVO.getId()).setFoodId(food);
            foodToComboList.add(foodToCombo);
        });
        foodToComboService.saveOrUpdateBatch(foodToComboList);}
        else {
            Combo combo = new Combo();
            combo.setId(comboVO.getId()).setName(comboVO.getName())
                    .setComboPic(comboVO.getComboPic())
                    .setMoney(comboVO.getMoney()).setDefaultMoney(comboVO.getDefaultMoney())
                    .setCinemaId(comboVO.getCinemaId())
                    .setSort(comboVO.getSort());
            this.saveOrUpdate(combo);
        }
        return true;
    }

}
